/*  This file is part of the comicconquer project
    Copyright (C) 2010 Reach <reach334@gmail.com>

    ComicConquer is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This software is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public 
    License along with this software.  If not, see <http://www.gnu.org/licenses/>.

*/

#ifndef GRAYSCALEDLG_H
#define GRAYSCALEDLG_H

#include "mist.h"
#include "QIplImage.h"
#include "MouseControl.h"
#include "onyx/ui/buttons.h"
#include "Configuration.h"

class GrayScaleDlg : public QDialog
{
    Q_OBJECT

    //-- macro for mouse controler
    CO_MOUSECONTROL
public slots:
    void mouseCtlEvent(GestureEvent* event);

    //-- constructor and destructor
public:
    GrayScaleDlg(QImage*imgSrc, GrayScaleSet& grayScaleSet, QWidget* parent = 0);
    ~GrayScaleDlg();

    //-- public methods
public:
    static GrayScaleSet setGrayScale(QImage* imgSrc, GrayScaleSet& grayScaleSet, QWidget* parent = 0);
    static void setGrayTable(QVector<QRgb>& rgbTable, const int iBlack, const float fMiddle, const int iWhite);
    bool event(QEvent* e);
    void refreshScreen(ScreenProxy::Waveform w = ScreenProxy::GC, bool whole = false){
        ScreenProxy::instance().updateWidget(this, w, whole);
    }

    //-- private methods
private:
    void drawHistogram();
    void setMiddleBlock(int xPos);
    void setMiddleBlock(float scaleMiddle);

    //-- private slots
private slots:
    void paintEvent(QPaintEvent* event);
    void reset();

    //-- private members
private:
    // widgets
    QHBoxLayout* m_hlayout;
    ui::OnyxPushButton* m_bOK;
    ui::OnyxPushButton* m_bReset;
    ui::OnyxPushButton* m_bCancel;

    // bar
#define GS_BLOCK_W 30
#define GS_BLOCK_H 40
#define GS_TEXT_H 16
    enum BlockSelect
    {
        BlockNone,
        BlockBlack,
        BlockWhite,
        BlockMiddle
    };
    int m_iGrayBarLen;
    int m_iGrayBarY;
    int m_iGrayBarStart;
    int m_iGrayBarEnd;
    QRect m_rcGrayBar;
    QRect m_rcSetting;
    QRect m_rcBlack;
    QRect m_rcWhite;
    QRect m_rcMiddle;
    QRect m_rcImage;
    BlockSelect m_blockSelect;
    int m_scaleBlack;
    int m_scaleWhite;
    float m_scaleMiddle;
    QVector<QRgb> m_rgbTable;

    // temporary image to display
    QImage* m_imgDst;
    QImage* m_imgHistogram;
};

#endif  // GRAYSCALEDLG_H
